
define(['jquery'],function () {
  var reqHome=function () {

    $.getJSON('../../data/home1.json',function(responseText,responseState,responseObj){

          console.log(responseText);
          jieXi(responseText);
        });
  }

      function jieXi(data) {
        //获取到标签
        var $homeDivs=$('.homeDiv');
        var $homegoods=$($homeDivs[4]).children('div');
        // console.log($homeDivs);
        // console.log($homeDivs.find('img'));

        var datas=data.data;
        var dataArr=datas.act_info;
        var m=-1;
        for(var i=0; i<dataArr.length; i++){

            if(dataArr[i].type=='hide'){
                m=m;
                continue;
            }else{
              m++;
            }

            var actArr=dataArr[i].act_rows;
            for(var j=0; j<actArr.length; j++){

              if(actArr[j].activity){
                var activity=actArr[j].activity;

                  if(!actArr[j].category_detail){
                    // console.log(actArr[j].category_detail);

                      $($($homeDivs[m]).find('img')[j]).attr('src',activity.img);

                      if($($($homeDivs[m]).find('img')[j]).next('p')){
                        $($($homeDivs[m]).find('img')[j]).next('p').html(activity.name);

                      }
                  }else{
                    // console.log('===');

                      var activity=actArr[j].activity;
                      var category_detail=actArr[j].category_detail;

                      // console.log($($homegoods[j]).children('.topImg'));
                      $($homegoods[j]).children('.topImg').children('img').attr('src',activity.img);
                      $($homegoods[j]).find('.title').html(category_detail.name);

                      for(var k=0; k<category_detail.goods.length; k++){
                        var goods=category_detail.goods[k];
                        // console.log('===');
                        // console.log($($($homegoods[j]).children('.goods').children('div')[k]).children('img'));
                        $($($homegoods[j]).children('.goods').children('div')[k]).children('img').attr('src',goods.img);
                        $($($homegoods[j]).children('.goods').children('div')[k]).children('.goodstitle').html(goods.keywords);
                        // $($($homegoods[j]).children('.goods').children('div')[k]).children('.fuli').children('span').html(goods.pm_desc);
                        $($($homegoods[j]).children('.goods').children('div')[k]).children('.fuli').children('span').html(goods.pm_desc);
                        $($($homegoods[j]).children('.goods').children('div')[k]).children('.weight').html(goods.specifics);
                        $($($homegoods[j]).children('.goods').children('div')[k]).children('.del').children('.newPrice').html('￥'+goods.partner_price);
                        $($($homegoods[j]).children('.goods').children('div')[k]).children('.del').children('.oldPrice').html('￥'+goods.market_price);
                      }
                  }

              }else {
                    var act_rows=actArr[j].act_rows;
              }

            }



        }
      }

  var homeHtml=function () {
      var $category=$('#category');
      for(var i=0; i<6; i++){
        var $divCreate=$('<div></div>').html("<div><p><!--<span></span>--><span class='title'>▍优选水果</span></p>"+
        "<p><span>更多</span><span> > </span></p></div>"+
      "<div class='topImg'><img src='./home/img/9k=.jpg'></div>"+"<div class='goods'></div>");

        $divCreate.addClass('home-goods');

        $category.append($divCreate);

      }

      for(var j=0; j<3; j++){
        var $divCre=$('<div></div>').html("<img src='./home/img/2Q==.jpg' />"+
        '<p class="goodstitle">爱鲜蜂.澳洲西梅</p>'+
        '<p class="icon"><span>精 选</span></p>'+
        '<p class="fuli"><span>买一赠一</span></p>'+
        '<p class="weight">400g以上/盒</p>'+
        '<p class="del">'+
          '<span class="newPrice">￥45.8</span>'+
          '<span class="oldPrice">￥55.8</span>'+
          '<img src="./home/img/icon-jia.jpg"  alt="添加"/>'+
      "</p>");

        $('.goods').append($divCre);
      }
  }


  var lunbo=function () {
      var $lunbot=$('#lunbotu');
      var $ul=$lunbot.children('ul');
      var $lia=$ul.children('li');

      var $li=$('<li></li>').html('<img src="http://img01.bqstatic.com//upload/activity/2017033110355935.jpg@90Q.jpg">');
      $ul.append($li);
      var $lis=$ul.children('li');

      var wid=$lunbot.width();
      // console.log($lis.length);
      $lis.width(wid);
      $ul.width($ul.children('li').length * wid);

      // var timer;
      var timer=setInterval(lunbotu,10);

      function lunbotu() {


        if($lunbot.scrollLeft() % wid == 0){
            clearInterval(timer);


            setTimeout(function () {
              timer=setInterval(lunbotu,10);
            },3000);

            if($lunbot.scrollLeft() == wid*2){
              $lunbot.scrollLeft(0);
            }
        }
        var x=$lunbot.scrollLeft()+1;
        $lunbot.scrollLeft(x);

      }

      
    }


    return{
      reqHome:reqHome,
      homeHtml:homeHtml,
      lunbo:lunbo
    }

});
